package com.xiaoshuidi.cloud.module.iot.controller.admin;

import cn.hutool.json.JSONUtil;
import com.xiaoshuidi.cloud.module.iot.controller.admin.doorlock.vo.OperateThirdDeviceRequestModel;
import com.xiaoshuidi.cloud.module.iot.controller.admin.doorlock.vo.OperateThirdDeviceResponseModel;
import com.xiaoshuidi.cloud.module.iot.controller.admin.doorlock.vo.PullThirdDeviceRequestModel;
import com.xiaoshuidi.cloud.module.iot.dal.model.AccessTokenRequestModel;
import com.xiaoshuidi.cloud.module.iot.dal.model.AccessTokenResponseModel;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbAccountDo;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbDeviceDo;
import com.xiaoshuidi.cloud.module.iot.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.iot.infrastructure.remote.TuYaDeviceConnector;
import com.xiaoshuidi.cloud.module.iot.service.impl.thirdPartImpl.TuYaInstanceServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：baohanzhong
 * @date ：Created 2024/9/5 12:22
 * @description：
 */
@RestController
@RequestMapping(ApiConstants.NAME+"/test/tuya")
@Slf4j
public class TestTuYaController {

    @Autowired
    private TuYaInstanceServiceImpl tuYaInstanceService;

    @Resource
    private TuYaDeviceConnector tuYaDeviceConnector;

    @GetMapping("testGetAccessToken")
    public void testGetAccessToken() throws Exception {
        AccessTokenRequestModel accessTokenRequestModel = new AccessTokenRequestModel();
        AccessTokenResponseModel accessToken = tuYaInstanceService.getAccessToken(accessTokenRequestModel);
        System.out.println( JSONUtil.toJsonStr(accessToken));
        log.info("testGetAccessToken=>result:{}", JSONUtil.toJsonStr(accessToken));
    }

    @GetMapping("testGetDevice")
    public void testGetDevice() throws Exception {
        PullThirdDeviceRequestModel pullThirdDeviceRequestModel = new PullThirdDeviceRequestModel();
        pullThirdDeviceRequestModel.setLoginName("ay1716262766359d1lXW");
        TbAccountDo tbAccountDo = new TbAccountDo();
        tbAccountDo.setApiNo("tuya_test");
        pullThirdDeviceRequestModel.setTbAccountDo(tbAccountDo);
        List<TbDeviceDo> device = tuYaInstanceService.getDevice(pullThirdDeviceRequestModel);
        System.out.println(JSONUtil.toJsonStr(device));
        log.info("testGetDevice=>result:{}", JSONUtil.toJsonStr(device));
    }

    @GetMapping("testAddDoorLockPassword")
    public void testAddDoorLockPassword() throws Exception {
        OperateThirdDeviceRequestModel operateThirdDeviceRequestModel = new OperateThirdDeviceRequestModel();
        operateThirdDeviceRequestModel.setDeviceThirdNo("6c093694255f2d917fpba9");
        operateThirdDeviceRequestModel.setPasswordStartTime(System.currentTimeMillis());
        operateThirdDeviceRequestModel.setPasswordEndTime(System.currentTimeMillis()+24*60*60*1000);
        operateThirdDeviceRequestModel.setMobile("18156069181");
        operateThirdDeviceRequestModel.setPasswordContent("946599");
        operateThirdDeviceRequestModel.setUserName("admin");
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = tuYaInstanceService.addDoorLockPassword(operateThirdDeviceRequestModel);
        System.out.println(JSONUtil.toJsonStr(operateThirdDeviceResponseModel));
        log.info("testAddDoorLockPassword=>result:{}", JSONUtil.toJsonStr(operateThirdDeviceResponseModel));
    }

    @GetMapping("testUpdateDoorLockPassword")
    public void testUpdateDoorLockPassword(@RequestBody OperateThirdDeviceRequestModel operateThirdDeviceRequestModel) throws Exception {
        operateThirdDeviceRequestModel.setPasswordStartTime(System.currentTimeMillis());
        operateThirdDeviceRequestModel.setPasswordEndTime(System.currentTimeMillis()+24*60*60*1000);
        operateThirdDeviceRequestModel.setMobile("18156069181");
        operateThirdDeviceRequestModel.setPasswordContent("946598");
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = tuYaInstanceService.updateDoorLockPassword(operateThirdDeviceRequestModel);
        System.out.println(JSONUtil.toJsonStr(operateThirdDeviceResponseModel));
        log.info("testUpdateDoorLockPassword=>result:{}", JSONUtil.toJsonStr(operateThirdDeviceResponseModel));
    }

    @GetMapping("testFrozenDoorLockPassword")
    public void testFrozenDoorLockPassword() throws Exception {
        OperateThirdDeviceRequestModel operateThirdDeviceRequestModel = new OperateThirdDeviceRequestModel();
        operateThirdDeviceRequestModel.setDeviceThirdNo("6c093694255f2d917fpba9");
        operateThirdDeviceRequestModel.setPasswordId("1239848712");
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = tuYaInstanceService.frozenDoorLockPassword(operateThirdDeviceRequestModel);
        System.out.println(JSONUtil.toJsonStr(operateThirdDeviceResponseModel));
        log.info("testFrozenDoorLockPassword=>result:{}", JSONUtil.toJsonStr(operateThirdDeviceResponseModel));
    }

    @GetMapping("testUnfrozenDoorLockPassword")
    public void testUnfrozenDoorLockPassword() throws Exception {
        OperateThirdDeviceRequestModel operateThirdDeviceRequestModel = new OperateThirdDeviceRequestModel();
        operateThirdDeviceRequestModel.setDeviceThirdNo("6c093694255f2d917fpba9");
        operateThirdDeviceRequestModel.setPasswordId("1239848712");
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = tuYaInstanceService.unfrozenDoorLockPassword(operateThirdDeviceRequestModel);
        System.out.println(JSONUtil.toJsonStr(operateThirdDeviceResponseModel));
        log.info("testUnfrozenDoorLockPassword=>result:{}", JSONUtil.toJsonStr(operateThirdDeviceResponseModel));
    }

    @GetMapping("testDeleteDoorLockPassword")
    public void testDeleteDoorLockPassword() throws Exception {
        OperateThirdDeviceRequestModel operateThirdDeviceRequestModel = new OperateThirdDeviceRequestModel();
        operateThirdDeviceRequestModel.setDeviceThirdNo("6c093694255f2d917fpba9");
        operateThirdDeviceRequestModel.setPasswordId("1239848712");
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = tuYaInstanceService.deleteDoorLockPassword(operateThirdDeviceRequestModel);
        System.out.println(JSONUtil.toJsonStr(operateThirdDeviceResponseModel));
        log.info("testDeleteDoorLockPassword=>result:{}", JSONUtil.toJsonStr(operateThirdDeviceResponseModel));
    }

    @GetMapping("testGetDoorLockStatus")
    public void testGetDoorLockStatus() throws Exception {
        OperateThirdDeviceRequestModel operateThirdDeviceRequestModel = new OperateThirdDeviceRequestModel();
        operateThirdDeviceRequestModel.setDeviceThirdNo("6c093694255f2d917fpba9");
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = tuYaInstanceService.getDoorLockStatus(operateThirdDeviceRequestModel);
        System.out.println(JSONUtil.toJsonStr(operateThirdDeviceResponseModel));
        log.info("testGetDoorLockStatus=>result:{}", JSONUtil.toJsonStr(operateThirdDeviceResponseModel));
    }

    @GetMapping("testGetOpenLockRecordes")
    public void testGetOpenLockRecordes() throws Exception {
        OperateThirdDeviceRequestModel operateThirdDeviceRequestModel = new OperateThirdDeviceRequestModel();
        operateThirdDeviceRequestModel.setDeviceThirdNo("6c093694255f2d917fpba9");
        operateThirdDeviceRequestModel.setPageNo(1);
        operateThirdDeviceRequestModel.setPageSize(10);
        operateThirdDeviceRequestModel.setQueryBeginTime(System.currentTimeMillis());
        operateThirdDeviceRequestModel.setQueryEndTime(System.currentTimeMillis()+24*60*60*1000);
        OperateThirdDeviceResponseModel operateThirdDeviceResponseModel = tuYaInstanceService.getOpenLockRecordes(operateThirdDeviceRequestModel);
        System.out.println(JSONUtil.toJsonStr(operateThirdDeviceResponseModel));
        log.info("testGetOpenLockRecordes=>result:{}", JSONUtil.toJsonStr(operateThirdDeviceResponseModel));
    }
}
